package com.come4loves.game.module.card.dao.impl;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.come4loves.core.dbcenter.dao.BaseDaoImpl;
import com.come4loves.game.dbproxy.entity.Card;
import com.come4loves.game.module.card.constant.CardType;
import com.come4loves.game.module.card.dao.CardDao;
import com.come4loves.game.module.card.model.CardModel;

@Repository
public class CardDaoImpl extends BaseDaoImpl implements CardDao {

	
	@Override
	public Card getCard(long actorId) {
		return get(actorId, Card.class);
	}

	@Override
	public CardModel getCardModel(long actorId, CardType cardType, int cfgId) {
		Card cards = get(actorId, Card.class);
		Map<Integer, CardModel> modelMap = null;
		switch (cardType) {
		case GENERAL:
			modelMap = cards.generCardsMap;
			break;
		case SPELL:
			modelMap = cards.spellCardsMap;
			break;
		case UNIT:
			modelMap = cards.unitCardsMap;
			break;
		case EQUIP:
			modelMap = cards.equipCardsMap;
			break;

		default:
			return null;
		}
		
		CardModel cardModel = modelMap.get(cfgId);
		return cardModel;
	}
	
	@Override
	public void putCardModel(long actorId, CardModel model) {
		Card cards = get(actorId, Card.class);
		Map<Integer, CardModel> modelMap = null;
		switch (model.type) {
		case GENERAL:
			modelMap = cards.generCardsMap;
			break;
		case SPELL:
			modelMap = cards.spellCardsMap;
			break;
		case UNIT:
			modelMap = cards.unitCardsMap;
			break;
		case EQUIP:
			modelMap = cards.equipCardsMap;
			break;
			
		default:
			return;
		}
		if (modelMap.containsKey(model.cfgId)) {
			CardModel oldModel = modelMap.get(model.cfgId);
			model.count += oldModel.count;
			modelMap.put(model.cfgId, model);
		} else {
			modelMap.put(model.cfgId, model);
		}
	}
}
